package com.wenresearch.orion.components;

import java.util.Timer;
import java.util.TimerTask;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

/**
 * Abstract class for Splash screen implementation
 * @author Ali Irawan (boylevantz@gmail.com)
 * @version 1.0
 */
public abstract class AbstractSplashScreen extends MainScreen {

	private MainScreen _next;
	private Timer _timer = new Timer();
	private UiApplication _app;

	/**
	 * Default constructor
	 * @param ui instance of UiApplication
	 * @param next screen to push after splash screen
	 */
	protected AbstractSplashScreen(UiApplication ui, MainScreen next) {
		//use fullscreen
		super(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT);

		this._app = ui;
		this._next = next;

		// create a timer to count down to the splash screens exit
		_timer.schedule(new ExitSplashCountDown(), getTimerInterval());
	}

	/**
	 * Get the bitmap to be drawn as Splash screen
	 * @return
	 */
	public abstract Bitmap getSplashBitmap();

	public abstract long getTimerInterval();

	/**
	 * <p>
	 * When the splash screen exits three things happen
	 * </p>
	 * <ol>
	 * <li>The timer is canceled.</li>
	 * <li>You pop the splash screen off the stack.</li>
	 * <li>You push the main screen onto the stack.</li>
	 * </ol>
	 */
	public void dismiss() {
		_timer.cancel();
		_app.popScreen(this);
		_app.pushScreen(_next);
	}

	/**
	 * <p>
	 * This {@link TimerTask} is used to make sure the splash screen exits and
	 * <b>does not display forever</b>.
	 * </p>
	 */
	private final class ExitSplashCountDown extends TimerTask {
		public void run() {
			ExitSplashThread dThread = new ExitSplashThread();
			_app.invokeLater(dThread);

		}
	}

	/**
	 * <p>
	 * Used by the {@link ExitSplashCountDown} to exit the splash screen.
	 * </p>
	 */
	private final class ExitSplashThread implements Runnable {
		public void run() {
			dismiss();
		}
	}

	/*
	 * When you click the navigation then the screen should exit and go to your
	 * applications main screen
	 * 
	 * (non-Javadoc)
	 * 
	 * @see net.rim.device.api.ui.Screen#navigationClick(int, int)
	 */
	protected boolean navigationClick(int status, int time) {
		dismiss();
		return true;
	}

	/*
	 * Make sure nothing happens when the navigation senses movement
	 * 
	 * (non-Javadoc)
	 * 
	 * @see net.rim.device.api.ui.Screen#navigationMovement(int, int, int, int)
	 */
	protected boolean navigationMovement(int dx, int dy, int status, int time) {
		return false;
	}

	/*
	 * Make sure nothing happens when unclick occurs
	 * 
	 * (non-Javadoc)
	 * 
	 * @see net.rim.device.api.ui.Screen#navigationUnclick(int, int)
	 */
	protected boolean navigationUnclick(int status, int time) {
		return false;
	}

	/**
	 * Paint the splash screen
	 */
	protected void paint(Graphics graphics) {
		// TODO Auto-generated method stub
		super.paint(graphics);
		Bitmap _bitmap = getSplashBitmap();

		//default is centered
		int x = (Display.getWidth() - _bitmap.getWidth()) / 2;
		int y = (Display.getHeight() - _bitmap.getHeight()) / 2;
		if (_bitmap != null) {
			graphics.drawBitmap(x, y, _bitmap.getWidth(), _bitmap.getHeight(), _bitmap, 0, 0);
		}
	}

}
